package me.frank.security.support;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @Author Frank Wang
 * @Description:
 * @Date 2018/12/11 17:19
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -5727504520530168396L;


    /**
     * 成功码.
     */
    public static final int SUCCESS_CODE = 200;

    /**
     * 成功信息.
     */
    public static final String SUCCESS_MESSAGE = "操作成功";

    /**
     * 错误码.
     */
    public static final int ERROR_CODE = 500;

    /**
     * 错误信息.
     */
    public static final String ERROR_MESSAGE = "内部异常";

    /**
     * 错误码：参数非法
     */
    public static final int ILLEGAL_ARGUMENT_CODE_ = 400;

    /**
     * 错误信息：参数非法
     */
    public static final String ILLEGAL_ARGUMENT_MESSAGE = "参数非法";



    /**
     * 编号.
     */
    private int code;

    /**
     * 信息.
     */
    private String message;

    /**
     * 结果数据
     */
    private T data;

    private Result(){
        this(SUCCESS_CODE,SUCCESS_MESSAGE,null);
    }


    private Result(int code, String message) {
        this(code,message,null);
    }

    private Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Result ok(){
        return new Result();
    }

    public static Result ok(String msg){
        return new Result(SUCCESS_CODE,msg);
    }

    public static <T> Result ok(T data){
        return new Result(SUCCESS_CODE,SUCCESS_MESSAGE,data);
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(Result.ERROR_CODE, StringUtils.isBlank(message) ? Result.ERROR_MESSAGE : message);
    }

    public static <T> Result<T> error() {
        return new Result<>(Result.ERROR_CODE, ERROR_MESSAGE);
    }

    public static  <T> Result wrap(int code,String message,T data){
        return new Result(code,message,data);
    }

    public static  <T> Result wrap(int code,String message){
        return new Result(code,message,null);
    }



    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
}
